package com.yuanchu.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.yuanchu.messages.service.MqMessageService;
import com.yuanchu.search.dto.OrderDetailDto;
import com.yuanchu.search.entity.YoshopOrderRefundEntity;
import com.yuanchu.search.service.RefreshOrderService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-29 22:49
 */
@Service
public class RefreshOrderServiceImpl implements RefreshOrderService {
    @Resource
    private RestHighLevelClient client;
    @Resource
    private MqMessageService mqMessageService;
    @Override
    public void refreshOrder(OrderDetailDto orderDetail, String message) {
        // 1.删除文档
        // 1.准备Request
        DeleteRequest deleteRequest = new DeleteRequest("yuanchu_order", String.valueOf(orderDetail.getOrderId()));
        // 2.发送请求
        try {
            client.delete(deleteRequest, RequestOptions.DEFAULT);
            // 3.添加新文档
            // 将HotelDoc转json
            String json = JSON.toJSONString(orderDetail);

            // 1.准备Request对象
            IndexRequest request = new IndexRequest("yuanchu_order").id(orderDetail.getOrderId().toString());
            // 2.准备Json文档
            request.source(json, XContentType.JSON);
            // 3.发送请求
            client.index(request, RequestOptions.DEFAULT);

        } catch (Exception e) {
            mqMessageService.addMessage("更新订单状态失败: yuanchu_order", message, null, null);
        }
    }

    @Override
    public void removeOrder(long orderId) {
        // 1.准备Request
        DeleteRequest request = new DeleteRequest("yuanchu_order", String.valueOf(orderId));
        // 2.发送请求
        try {
            client.delete(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            mqMessageService.addMessage("删除订单文档失败: yuanchu_order", String.valueOf(orderId), null, null);
        }
    }

    @Override
    public void refreshOrderRefund(YoshopOrderRefundEntity orderRefund, String message) {
        // 1.删除文档
        // 1.准备Request
        DeleteRequest deleteRequest = new DeleteRequest("yuanchu_order_refund", String.valueOf(orderRefund.getOrderRefundId()));
        // 2.发送请求
        try {
            client.delete(deleteRequest, RequestOptions.DEFAULT);
            // 3.添加新文档
            // 将HotelDoc转json
            String json = JSON.toJSONString(orderRefund);

            // 1.准备Request对象
            IndexRequest request = new IndexRequest("yuanchu_order_refund").id(orderRefund.getOrderRefundId().toString());
            // 2.准备Json文档
            request.source(json, XContentType.JSON);
            // 3.发送请求
            client.index(request, RequestOptions.DEFAULT);

        } catch (Exception e) {
            mqMessageService.addMessage("更新订单文档失败: yuanchu_order_refund", message, null, null);
        }
    }
}
